package com.saunter.filter;

import com.saunter.utils.CookieUtils;
import com.saunter.utils.encrypt.JavaWebToken;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(urlPatterns = {"/user/*"},filterName = "userFilter")
public class UserFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request= (HttpServletRequest) servletRequest;
        if(!"/user/login".equals(request.getRequestURI())) {
            Object id_num = JavaWebToken.parseJavaWebToken(CookieUtils.getCookie(request, "sid"), "id_num");
            CusRequestWrapper cusRequestWrapper = new CusRequestWrapper(request);
            cusRequestWrapper.addParameterMap("system_append_id_num", null == id_num ? null : id_num.toString());
            filterChain.doFilter(cusRequestWrapper,servletResponse);
        }else {
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
